package org.qloudgen.borg.pages;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.DataSourceBean;
import org.qloudgen.borg.beans.FileUploadBean;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.compnent.CancelButton;
import org.qloudgen.borg.pages.panel.DSFileUploadPanel;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@SuppressWarnings({"unchecked","rawtypes"})
public class DataSourceFuncCreatePage extends BasePage {
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */
	static Log log = LogFactory.getLog(DataSourceFuncCreatePage.class);
	
	private static final long serialVersionUID = 1L;
	private transient Map<String,String> funcMap = Maps.newHashMap();
	private transient List<FuncBean> funcs = Lists.newArrayList();
	private transient List<String> funcList = Lists.newArrayList();
	private transient String dsNo ,dsName;
	
	public DataSourceFuncCreatePage(final PageParameters pageParameters){
		
		super();
		
		// get dsNo
		dsNo = pageParameters.get("dsNo").toString();
		dsName = pageParameters.get("dsName").toString();

		final DSFileUploadPanel transformPanel = new DSFileUploadPanel("transformPanel","数据转换脚本：" );
		
		Form<?> form = new Form("form",new CompoundPropertyModel<DataSourceBean>(new DataSourceBean().setDsName(dsName))){
			
			private static final long serialVersionUID = 1L;
			protected void onSubmit() {
				
				// get form obj
				DataSourceBean ds = (DataSourceBean)getDefaultModelObject();
				FileUploadBean transform = (FileUploadBean) transformPanel.get("panel").getDefaultModelObject();
				
				if (transform.getContents() == null) {
					info(CommonValues.INFO_NOT_NULL);
					return;
				}
				
				ds.setTransformFile(transform.getContents());
				ds.setDsNo(dsNo);
				
				// insert into metadb
				DefaultImpl.createDataSourceFunc(ds);
				setResponsePage(DataSourceFuncListPage.class,pageParameters);
			}
			
		};
		form.setOutputMarkupId(true);
		this.add(form);
		
		form.add(new TextField<String>("dsName").setEnabled(false));
		
		//get funcs from metadb
		DefaultImpl.getDsAvailableFuncList(dsNo, funcs);
		
		//get app map from funcs
		getMap(funcs);

		IChoiceRenderer<Object> funcRender = new ChoiceRenderer<Object>(){
			private static final long serialVersionUID = 1L;
			public Object getDisplayValue(Object object) {
				return funcMap.get(object);
			}
		};
		
		final DropDownChoice<String> func = new DropDownChoice<String>("funcNo", funcList, funcRender);
		func.setOutputMarkupId(true);
		func.setRequired(true);
		func.setNullValid(false);
		
		form.add(func);
		form.add(transformPanel);
		form.add(new Button("submit"));
		form.add(new CancelButton("cancel",DataSourceFuncListPage.class,pageParameters));
		
	}

	private void getMap(List<FuncBean> funcs) {
		
		for(FuncBean func:funcs){
			funcMap.put(func.getFuncNo(), func.getFuncName());
			funcList.add(func.getFuncNo());
		}
	}
}

